package com.android.questions.ui.adapter;

import android.annotation.SuppressLint;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import com.android.base.util.ActivityClickUtil;
import com.android.questions.R;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hx.currency.data.qa.model.QAPushApp;

import org.raphets.roundimageview.RoundImageView;

/**
 * FileName: PushAdapter
 * Author: admin
 * Date: 2021/11/4 15:07
 * Description:
 */
public class PushAdapter extends BaseQuickAdapter<QAPushApp, BaseViewHolder> {
    private OnItemChildClickLister clickLister;

    public PushAdapter() {
        super(R.layout.item_push);
    }

    @Override
    protected int getDefItemViewType(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(final BaseViewHolder baseViewHolder, QAPushApp item) {
        RoundImageView ivIcon = baseViewHolder.getView(R.id.item_icon);
        AppCompatTextView tvName = baseViewHolder.getView(R.id.item_name);
        AppCompatTextView tvCash = baseViewHolder.getView(R.id.item_cash);
        AppCompatTextView tvProgress = baseViewHolder.getView(R.id.item_tv_progress);
        AppCompatImageView ivReceive = baseViewHolder.getView(R.id.item_receive);

        Glide.with(ivIcon)
                .load(item.getIul())
                .placeholder(R.drawable.icon_push_default_icon)
                .error(R.drawable.icon_push_default_icon)
                .into(ivIcon);

        String name = item.getYym();
        if (!TextUtils.isEmpty(name) && name.contains("(")) {
            name = name.substring(0, name.indexOf("("));
        }
        if (!TextUtils.isEmpty(name)) {
            tvName.setText(name);
        }

        String cash = "可领<b>" + item.getRa() + "</b>元";
        tvCash.setText(Html.fromHtml(cash));

        String s = "下载后观看<b><font color='#FD0D21'>" + item.getVn() + "</font></b>次激励视频";
        tvProgress.setText(Html.fromHtml(s));

        final int state = item.getZt();
        if (state == 0) {
            ivReceive.setEnabled(true);
            ivReceive.setImageResource(R.drawable.icon_push_download);
        } else if (state == 1) {
            ivReceive.setEnabled(true);
            ivReceive.setImageResource(R.drawable.icon_push_wait_complete);
        } else if (state == 2) {
            ivReceive.setEnabled(true);
            ivReceive.setImageResource(R.drawable.icon_push_withdrawal);
        } else {
            ivReceive.setEnabled(false);
            ivReceive.setImageResource(R.drawable.icon_push_withdrawal_complete);
        }

        ivReceive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ActivityClickUtil.getInstance().isFastClick()) {
                    //防止用户快速点击
                    return;
                }
                if (clickLister != null) {
                    if (state == 0) {
                        clickLister.onDownload(item);
                    } else if (state == 1) {
                        clickLister.onConfirm(item);
                    } else if (state == 2) {
                        int position = getItemPosition(item);
                        clickLister.onWithdrawal(item, position);
                    }
                }
            }
        });
    }

    public OnItemChildClickLister getClickLister() {
        return clickLister;
    }

    public void setClickLister(OnItemChildClickLister clickLister) {
        this.clickLister = clickLister;
    }

    public interface OnItemChildClickLister {
        void onDownload(QAPushApp item);

        void onConfirm(QAPushApp item);

        void onWithdrawal(QAPushApp item, int pos);
    }
}